package com.coolgor.axnut.schedule.mdmis;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.coolgor.axnut.dto.mdmis.DeliverInfoExecution;
import com.coolgor.axnut.dto.mdmis.PolicyExecution;
import com.coolgor.axnut.dto.mdmis.TaskTraceExecution;
import com.coolgor.axnut.dto.wechat.qyh.QYHMessageExecution;
import com.coolgor.axnut.entity.mdmis.DeliverInfoCondition;
import com.coolgor.axnut.entity.mdmis.PolicyCondition;
import com.coolgor.axnut.entity.mdmis.TaskTrace;
import com.coolgor.axnut.entity.mdmis.TaskTraceCondition;
import com.coolgor.axnut.enums.mdmis.PolicyOperationStateEnum;
import com.coolgor.axnut.enums.wechat.qyh.QYHMessageOperationStateEnum;
import com.coolgor.axnut.service.mdmis.DeliverInfoService;
import com.coolgor.axnut.service.mdmis.PolicyService;
import com.coolgor.axnut.service.mdmis.PolicyStatisticsService;
import com.coolgor.axnut.service.mdmis.TaskTraceService;
import com.coolgor.axnut.service.wechat.qyh.QYHMessageService;



@Component
public class DailyReportTask {

	@Autowired
	private TaskTraceService taskTraceService;	

	@Autowired
	private PolicyService policyService;

	@Autowired
	private DeliverInfoService deliverInfoService;

	@Autowired
	private PolicyStatisticsService policyStatisticsService;	
	
	@Autowired
	private QYHMessageService qyhMessageService;		

	
	/** 每天晚上21:00、22:00、23:00
	 *  统计当天配送数据
	 *  通过企业微信发送简讯
	 */ // http://cron.qqe2.com/
//	@Scheduled(cron = "0 0/1 * * * ? ") //测试用 每分钟执行一次
	@Scheduled(cron = "0 1 21,22 * * ? ")
    public void wechatDailyReport() {
		TaskTraceCondition taskTraceCondition = new TaskTraceCondition();
		taskTraceCondition.setTaskName("微信推送当日配送简报");
		taskTraceCondition.setTaskTimeBegin(new Date(System.currentTimeMillis()));
		taskTraceCondition.setTaskTimeEnd(new Date(System.currentTimeMillis()));
		taskTraceCondition.setResult("success");
		TaskTraceExecution taskTraceExecution = taskTraceService.getTaskTraceList(taskTraceCondition, 0, 9999);
		if (taskTraceExecution.getCount() == 0) {
			TaskTrace taskTrace = new TaskTrace();
			taskTrace.setTaskID(2L);
			taskTrace.setTaskName("微信推送当日配送简报");
			taskTrace.setTaskTime(new Date(System.currentTimeMillis()));
			String operation = "";
			String result = "";
			
			//统计今日寄送量
			DeliverInfoCondition deliverInfoCondition = new DeliverInfoCondition();
			deliverInfoCondition.setDeliverTimeBegin(new Date(System.currentTimeMillis()));
			deliverInfoCondition.setDeliverTimeEnd(new Date(System.currentTimeMillis()));
			DeliverInfoExecution deliverInfoExe = deliverInfoService.getDeliverInfoList(deliverInfoCondition, 0, 9);
			//判断今日是否有配送信息
			if (deliverInfoExe.getCount()>0) {
				//如果有有寄送信息
				operation += "今日寄出：" + deliverInfoExe.getCount() + "件\\n";

				PolicyCondition policyCondition = new PolicyCondition();				
				Calendar cale;
				DecimalFormat df = new DecimalFormat("#.##%");
				List<Map<String, Object>> list ;
				
				//仅统计纸质保单
				policyCondition.setPolicyType("纸质保单");
				
				//统计今日回销单量           
				policyCondition.setReceiptTimeBegin(new Date(System.currentTimeMillis()));
				policyCondition.setReceiptTimeEnd(new Date(System.currentTimeMillis()));
				PolicyExecution policyExe = policyService.getPolicyList(policyCondition, 0, 9);
				if (policyExe.getState()==PolicyOperationStateEnum.SUCCESS.getState()) {
					operation += "今日回销：" + policyExe.getCount() + "件\\n";					
				}	
				//条件用完了记得清空
				policyCondition.setReceiptTimeBegin(null);
				policyCondition.setReceiptTimeEnd(null);						
				
				//统计本月配送情况
				//获取本月的第一天
				cale = Calendar.getInstance();//获取当前日期  
				cale.add(Calendar.MONTH, 0);
				cale.set(Calendar.DAY_OF_MONTH,1);
//				policyCondition.setInsureTimeBegin(cale.getTime());//设置投保日期为统计起点
				policyCondition.setIntroTimeBegin(cale.getTime());//设置接件日期为统计起点
				//获取本月的最后一天
				cale = Calendar.getInstance();
		        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));  
//				policyCondition.setInsureTimeEnd(cale.getTime());//设置投保日期为统计终点
				policyCondition.setIntroTimeEnd(cale.getTime());//设置接件日期为统计起点
				list = policyStatisticsService.countReceiptRateByProject(policyCondition);
				if (list!=null)  {
					for (Map<String, Object> m : list) {
						if ( m.get("project").equals("广西汇总") ){
							operation += "本月投保：" + m.get("total") + "件\\n";
							operation += "本月回销：" + m.get("receipt") + "件\\n";
							operation += "本月回销率：" + df.format(m.get("receipt_rate")) + "\\n";
						}
					}
				}	
				//统计上月配送情况
				//获取前月的第一天
				cale = Calendar.getInstance();//获取当前日期 
				cale.add(Calendar.MONTH, -1);
				cale.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
//				policyCondition.setInsureTimeBegin(cale.getTime());//设置投保日期为统计起点
				policyCondition.setIntroTimeBegin(cale.getTime());//设置接件日期为统计起点
				//获取前月的最后一天
				cale = Calendar.getInstance();   
		        cale.set(Calendar.DAY_OF_MONTH,0);//设置为0号,本月1号的前一天 
//				policyCondition.setInsureTimeEnd(cale.getTime());//设置投保日期为统计终点
				policyCondition.setIntroTimeEnd(cale.getTime());//设置接件日期为统计起点
				list = policyStatisticsService.countReceiptRateByProject(policyCondition);
				if (list!=null)  {
					for (Map<String, Object> m: list) {
						if ( m.get("project").equals("广西汇总") ){
							operation += "上月投保：" + m.get("total") + "件\\n";
							operation += "上月回销：" + m.get("receipt") + "件\\n";
							operation += "上月回销率：" + df.format(m.get("receipt_rate")) + "\\n";
						}
					}
				}				
				
				//添加当天是否存在相同电话的保单的检查结果信息
				taskTraceCondition.setTaskName("检查当天是否存在相同电话的保单");
				taskTraceExecution = taskTraceService.getTaskTraceList(taskTraceCondition, 0, 9999);
				if (taskTraceExecution.getCount() > 0) {
					//取第一个值
					operation += taskTraceExecution.getTaskTraceList().get(0).getOperation()+ "\\n";
				}
				
				//在企业号中进行任务报告
				operation = "<))MACE任务报告((>\\n【广西今日配送情况】\\n仅统计纸质保单数据\\n" + operation ;
				//在广西太平企业号的【老法师】中发送信息
				QYHMessageExecution qyhMessageExe1 = qyhMessageService.sendText(12L, "@all", null, null, operation, false);	
				//在盛古科技企业号的AIRMACE中发送信息
				//QYHMessageExecution qyhMessageExe2 = qyhMessageService.sendText(13L, "@all", null, null, operation, false);			
				if (qyhMessageExe1.getState()==QYHMessageOperationStateEnum.SUCCESS.getState()) {
					result = "success" ;								
				} else {
					operation = "企业号信息推送异常！";
					result = "false" ;				
				}				
			} else {
				//如果没有寄送信息，估计今天是休息日，就不要推送信息了！
				operation = "今日没有寄送保单\\n";
				result = "success" ;								
			}	
			
			taskTrace.setOperation(operation);
			taskTrace.setResult(result);
			taskTraceService.addTaskTrace(taskTrace);
		} 
    }
}
